﻿using UnityEngine;
using System.Collections;
using TileMapClasses;
using ResourceClasses;

namespace GUIElements
{
	/// <summary>
	/// Resource view.
	/// </summary>
	public class ResourceView : MonoBehaviour
	{
		//Private Class Variables
		private bool visible;
		private static ResourceView m_instance;

		//Arrays for Resources
		private Resource[] resList;
		private Resource[] resPerTurn;

		//Variables to set Resource Boxes.
		private string hoverSelector;
		private GUILayoutOption width;
		private GUILayoutOption height;

		//Public variables for the boxes.
		public GUIStyle WindowStyle;
		public GUIStyle boxStyle;
		public GUISkin Skin;

		/// <summary>
		/// Constructer for this class
		/// </summary>
		void Awake ()
		{
			//This sets all the variables.
			m_instance = this;
			visible = true;
			width = GUILayout.Width (75);
			height = GUILayout.Height (50);
			resList = new Resource[5];
			resPerTurn = new Resource[5];

			resList [0] = new Resource ("People", 0);
			resList [1] = new Resource ("Food", 0);
			resList [2] = new Resource ("Ore", 0);
			resList [3] = new Resource ("Stone", 0);
			resList [4] = new Resource ("Lumber", 0);

			resPerTurn [0] = new Resource ("People", 0);
			resPerTurn [1] = new Resource ("Food", 0);
			resPerTurn [2] = new Resource ("Ore", 0);
			resPerTurn [3] = new Resource ("Stone", 0);
			resPerTurn [4] = new Resource ("Lumber", 0);

			Tile.tileControlUpdate += tileControlListener;
		}

		/// <summary>
		/// Cleans up the event handler.
		/// </summary>
		void OnDestroy ()
		{
			Tile.tileControlUpdate -= tileControlListener;
		}

		/// <summary>
		/// Resets the resource.
		/// </summary>
		private void ResetResource ()
		{
			for (int i = 0; i < 5; i++) {
				resPerTurn [i].Amount = 0;
			}
		}

		/// <summary>
		/// Ends the turn combine.
		/// </summary>
		public void EndTurnCombine ()
		{
			for (int i = 0; i < 5; i++) {
				resList [i].Amount += resPerTurn [i].Amount;
			}
		}

		/// <summary>
		/// Tiles the control listener.
		/// </summary>
		/// <param name="state">State.</param>
		/// <param name="tileID">Tile ID.</param>
		public void tileControlListener (int state, int tileID)
		{
			ResetResource ();
			for (int i = 0; i < tileManager.Instance.TileMap.Size; i++) {
				Tile temp = tileManager.Instance [i];
				if (temp.Controlled == 1) {
					resInfo (temp.resAcc);
				}
			}
		}

		/// <summary>
		/// Changes the info per turn in the resource view.
		/// </summary>
		/// <param name="res">Res</param>
		private void resInfo (Resource res)
		{
			if (res.Name == resPerTurn [0].Name) {
				resPerTurn [0].Amount += res.Amount;
			} else if (res.Name == resPerTurn [1].Name) {
				resPerTurn [1].Amount += res.Amount;
			} else if (res.Name == resPerTurn [2].Name) {
				resPerTurn [2].Amount += res.Amount;
			} else if (res.Name == resPerTurn [3].Name) {
				resPerTurn [3].Amount += res.Amount;
			} else if (res.Name == resPerTurn [4].Name) {
				resPerTurn [4].Amount += res.Amount;
			}
		}

		/// <summary>
		/// Draws the GUI for the resource view.
		/// </summary>
		void OnGUI ()
		{
			GUI.skin = Skin;
			if (visible) {
				//Creates the Button to the correct size, Adds the info for Button,Tooltip
				GUILayout.BeginHorizontal (GUILayout.Width (375), GUILayout.Height (50));
				for (int i = 0; i < 5; i++) {
					GUILayout.Box (new GUIContent (resList [i].Amount.ToString (), resList [i].Name), WindowStyle, width, height);
				}
				GUILayout.EndHorizontal ();

				hoverSelector = GUI.tooltip;

				Rect position = new Rect (10, 50, 75, 50);
				for (int i = 0; i < 5; i++) {
					if (hoverSelector == resList [i].Name) {
						drawLabel (position, i);
					}
					position.x += 75;
				}
			}
		} 

		/// <summary>
		/// Draws the label.
		/// </summary>
		/// <param name="position">Position.</param>
		/// <param name="type">Type.</param>
		private void drawLabel (Rect position, int type)
		{
			GUI.Label (position, resPerTurn [type].Amount.ToString () + " Per Turn", boxStyle);
		}

		/// <summary>
		/// Gets or sets the <see cref="GUIElements.ResourceView"/> at the specified index.
		/// </summary>
		/// <param name="index">Index.</param>
		public Resource this [int index] {
			get {
				return resList [index];
			}
			set {
				resList [index].Amount = value.Amount;
			}
		}

		/// <summary>
		/// Gets the instance.
		/// </summary>
		/// <value>The instance.</value>
		public static ResourceView Instance {
			get {
				return m_instance;
			}
		}
	}
}
